#include "task.hpp"
#include "server.hpp"

void server_del(int sockfd, request &req, response &resp)
{
    switch (req._op)
    {
    case '+':
        resp._exitcode = OK;
        resp._result = req._x + req._y;
        break;
    case '-':
        resp._exitcode = OK;
        resp._result = req._x - req._y;
        break;
    case '*':
        resp._exitcode = OK;
        resp._result = req._x * req._y;
        break;
    case '/':
        if (req._y == 0)
            resp._exitcode = DIV_ZERO;
        else
        {
            resp._result = req._x / req._y;
            resp._exitcode = OK;
        }
        break;
    case '%':
        if (req._y == 0)
            resp._exitcode = MOD_ZERO;
        else
        {
            resp._result = req._x / req._y;
            resp._exitcode = OK;
        }
        break;
    default:
        resp._exitcode = OP_ERROR;
        logMessage(ERROR, "get request operator error");
        break;
    }
}

void user_mandb()
{
    std::cout << "error command, please input like:  \"./server port\" " << std::endl;
}

int main(int args, char *argc[])
{
    if (args != 2)
    {
        user_mandb();
    }

    tcp_server *server = new tcp_server(atoi(argc[1]));
    server->server_init();
    server->server_start(server_del);

    delete server;
    return 0;
}
